<template>
    <div>

        <Modal v-model="visible02" :title="title" width="800" style="position:relative;">
            <Form ref="formData02" :model="formData" :label-width="150" :rules="rules02" :disabled="disableSubmit02">
                <table>
                    <tr>
                        <td>
                            <FormItem label="姓名" prop="ssde0001">
                                <Input v-model="formData.ssde0001" placeholder="" style="width: 200px" :disabled="editable"/>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="身份证号" prop="ssde0002">
                                <Input v-model="formData.ssde0002" placeholder="" style="width: 200px" :disabled="editable"/>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <Form-item label="属地区划:" prop="ssde0009">
                                <area-linkage :areaCode.sync="formData.ssde0009" :countyDisable=countyState
                                              :streetDisable=streetState
                                              :communityDisable=communityState></area-linkage>
                            </Form-item>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="专项补贴发放年月" prop="ssde0003">
                                <DatePicker type="month" v-model="formData.ssde0003" placeholder="请选择专项补贴发放年月"
                                            @on-change="checkSs06007" :disabled="editable"
                                            style="width: 200px" />
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="专项救助类别" prop="ssde0004">
                                <Select v-model="formData.ssde0004" style="width:200px" @on-change="changeSsde0004" :disabled="editable">
                                    <Option value="3070">电价补贴对象</Option>
                                    <Option value="3080">价格补贴对象</Option>
                                </Select>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="救助对象类别" prop="ssde0008">
                                <Select v-model="formData.ssde0008" style="width:200px" @on-change="checkSs06007" :disabled="editable">
                                    <Option value="1010">城市低保对象</Option>
                                    <Option value="1020">城市低保边缘家庭</Option>
                                    <Option value="1030">农村低保对象</Option>
                                    <Option value="1040">农村低保边缘家庭</Option>
                                </Select>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="救助标准类别" prop="ssde0005">
                                <Select v-model="formData.ssde0005" style="width:200px" disabled="disabled">
                                    <Option value="1">按人</Option>
                                    <Option value="2">按户</Option>
                                </Select>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="救助标准" prop="ssde0006">
                                <Input v-model="formData.ssde0006" placeholder="" style="width: 200px" number readonly/>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="救助金额" prop="ssde0007">
                                <Input v-model="formData.ssde0007" placeholder="" style="width: 200px" number/>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="银行账号" prop="ssde0016">
                                <Input v-model="formData.ssde0016" placeholder="" style="width: 200px" number/>
                            </FormItem>
                        </td>
                    </tr>
                </table>
            </Form>
            <div slot="footer">
                <Button @click="visible02 = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData02')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import DateFormat from '@/plugins/format.js'
    import {IdentityCodeValid} from '@/plugins/idcardValid.js'


    export default {
        mixins: [CommonMixin],
        components: {},
        data() {
            return {
                countyState: false,
                streetState: false,
                communityState: false,
                editable: false,
                fieldsToValidate02: ['ssde0001', 'ssde0002', 'ssde0003', 'ssde0004', 'ssde0005', 'ssde0006', 'ssde0007', 'ssde0008', 'ssde0009', 'ssde0016'],
                rules02: {
                    ssde0001: [
                        {required: true, message: '请选输入姓名', trigger: 'blur'}
                    ],
                    ssde0002: [
                        {required: true, message: '请输入身份证号', trigger: 'blur'}
                    ],
                    ssde0003: [
                        {required: true, message: '请选择专项补贴发放年月', trigger: 'blur', type: 'date'}
                    ],
                    // ssfc0002: [
                    //     {required: true, message: '请选择花名册救助年月', trigger: 'blur', type: 'date'}
                    // ],
                    ssde0004: [
                        {required: true, message: '请选择专项救助类别', trigger: 'blur'},
                    ],
                    ssde0005: [
                        {required: true, message: '请选择救助标准类型', trigger: 'blur'},
                    ],
                    ssde0006: [
                        {required: true, message: '请输入救助标准', trigger: 'blur', type: 'number'},
                    ],
                    ssde0007: [
                        {required: true, message: '请输入救助金额', trigger: 'blur', type: 'number'},
                    ],
                    ssde0008: [
                        {required: true, message: '请选择救助对象类别', trigger: 'blur'},
                    ],
                    ssde0009: [
                        {required: true, message: '请选择属地区划', trigger: 'blur'},
                    ],
                    ssde0016: [
                        {required: true, message: '请输入银行账号', trigger: 'blur', type: 'number'},
                    ],
                },
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss13008Service/getPageList",
                    add: "/api/ac/subsistence/ss13008Service/insertData",
                    edit: "/api/ac/subsistence/ss13008Service/updateData",
                    delete: "/api/ac/subsistence/ss13008Service/deleteData",
                },
                formData: {},
                queryParam: {},
                visible: false,
                visible02: false,
                showSubmit: true,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: "",
                ssde0006Return: null,
                disableSubmit02: false,
            }
        },
        methods: {
            selectChange({row}) {
                this.$refs.xTree.setRadioRow(row);
                this.selectedData = row;
            },
            handleAdd() {
                this.$refs.formData02.resetFields();
                let that = this;
                that.disableSubmit02 = false
                that.title = "录入"
                let record = {};
                that.visible02 = true
                that.showSubmit = true
                that.formData = Object.assign({}, record)
                that.countyState = false
                that.streetState = false
                that.communityState = false
                that.editable = false
            },
            handleEdit(row) {
                this.$refs.formData02.resetFields();
                let that = this;
                that.countyState = true
                that.streetState = true
                that.communityState = true
                that.editable = true
                that.disableSubmit02 = false
                if (row == '' || row == undefined) {
                    that.$Message.error('请选中一条数据!')
                    return false;
                }
                that.title = "编辑"
                let record = row;
                if (record.id) {
                    that.visible02 = true
                    that.showSubmit = true
                    that.formData = Object.assign({}, record)
                    if (that.formData.ssde0003) {
                        that.formData.ssde0003 = new Date(record.ssde0003);
                    }
                } else {
                    that.visible02 = false
                }
            },
            handleView(row) {
                this.$refs.formData02.resetFields();
                let that = this;
                if (row == '' || row == undefined) {
                    that.$Message.error('请选中一条数据!')
                    return false;
                }
                that.title = "查看"
                let record = row;
                if (record.id) {
                    that.disableSubmit02 = true
                    that.visible02 = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.ssde0003 = new Date(record.ssde0003);
                } else {
                    that.visible02 = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];
                that.Loading = true;
                this.$Spin.show();
                for (let item of that.fieldsToValidate02) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(error);
                        }
                    })
                }
                if (validate) {
                    let that = this;
                    let url = null;
                    if (that.formData.id == null) {
                        url = that.url.add
                    } else {
                        url = that.url.edit
                    }
                    if (that.formData.ssde0003 != undefined) {
                        that.formData.ssde0003 = DateFormat(that.formData.ssde0003, 'YYYY-MM')
                    }
                    postAction(url, that.formData).then((res) => {
                        if (res.data.errcode == 0 && res.data.data.success == '1'){
                            this.$Message.success(res.data.data.msg)
                            this.$emit('ok');
                        } else {
                            this.$Message.error(res.data.data.msg);
                        }
                    }).finally(() => {
                        // 以下代码不生效
                        that.visible = false
                        this.loadData();
                        that.Loading = false;
                        this.$Spin.hide();
                        that.selectedData = null
                    })
                } else {
                    that.$Message.error(err_msg[0]);
                    that.Loading = false;
                    this.$Spin.hide();
                    return
                }
            },
            changeSsde0004() { //专项救助类别
                let that = this
                if (that.formData.ssde0004 == '3070') { //电价补贴对象
                    that.formData.ssde0005 = '2' //按户计算
                }
                if (that.formData.ssde0004 == '3080') { //价格补贴对象
                    that.formData.ssde0005 = '1' //按人计算
                }
                this.checkSs06007()
            },
            checkSs06007() { //查询指定月份及专项补贴类别是否存在标准记录
                let that = this
                if (that.formData.ssde0003 != '' && that.formData.ssde0004 != '' && that.formData.ssde0008 != ''
                    && that.formData.ssde0003 != undefined && that.formData.ssde0004 != undefined && that.formData.ssde0008 != undefined) {
                    let dataParam = {};
                    dataParam.cfag0004 = DateFormat(that.formData.ssde0003, 'YYYY-MM')
                    if (that.formData.ssde0004 == '3070') { //电价补贴, 对应festival是2
                        dataParam.cfag0001 = '2'
                    } else {
                        dataParam.cfag0001 = '3'
                    }
                    postAction('/api/ac/subsistence/ss06007Service/check', dataParam).then((res) => {
                        if (res.data.data.success == '1' && res.data.errcode == 0) {
                            if (that.formData.ssde0008 == '1010' || that.formData.ssde0008 == '1030') {
                                this.$set(this.formData, "ssde0006", res.data.data.result.cfag0002);
                            } else {
                                this.$set(this.formData, "ssde0006", res.data.data.result.cfag0003);
                            }
                        } else if (res.data.data.success == '0' && res.data.errcode == 0) {
                            that.$Message.error(res.data.data.msg);
                        }
                    });
                }
            }
        },
        created() {},
        updated() {}
    }
</script>
<style scoped>
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }

    /deep/ .ivu-table .demo-table-info-cell-age {
        background-color: #ff6600;
        color: #fff;
    }

    /deep/ .ivu-table .demo-table-info-cell-ssfc0015_name {
        background-color: #f33e3e;
        color: #fff;
    }
</style>
